import pymysql


class Mysqldb:
    def __init__(self, tableName):
        self.db = pymysql.connect(host='localhost',
                                  user='root',
                                  password='ff123456',
                                  port=3306,
                                  db='steam')
        self.cursor = self.db.cursor()
        self.tableName = tableName
        self.count = self.Update_Count()

    def Store_data(self, data):
        keys = ','.join(data.keys())
        values = ','.join(['%s'] * len(data))
        sql = 'INSERT INTO {table}({keys}) VALUES ({values})'.format(table=self.tableName, keys=keys, values=values)
        print(sql)
        print(tuple(data.values()))
        try:
            if self.cursor.execute(sql, tuple(data.values())):
                print('Successful')
                self.db.commit()
                self.count += 1
        except Exception as e:
            print(e)
            self.db.rollback()

    def Update_Count(self):
        sql = "SELECT COUNT(*) from " + self.tableName
        try:
            self.cursor.execute(sql)
            count = self.cursor.fetchone()
            return count[0]
        except Exception as e:
            print(e)

    def Get_Count(self):
        return self.count

    def __del__(self):
        self.db.close()


class briefingsOfGame:
    def __init__(self, name, appId, url, sortRank):
        self.name = name
        self.appId = appId
        self.url = url
        self.sortRank = sortRank

    def ConstructDir(self):
        return {
            "name": self.name,
            "appId": self.appId,
            "url": self.url,
            "sortRank": self.sortRank
        }

    def SetRank(self, sortRank):
        self.sortRank = sortRank
